<template>
    <div class="app-container">
        <table-header-component v-on:create="create" v-on:getList="getList">
            <el-input v-model="searchForm.user_name" placeholder="请输入名字" style="width: 200px;"></el-input>
            <!--<el-select v-model="searchForm.status" placeholder="请选择状态">-->
                <!--<el-option label="全部" value=""></el-option>-->
                <!--<el-option v-for="item in options.status" :key="item.value" :label="item.text" :value="item.value"></el-option>-->
            <!--</el-select>-->
        </table-header-component>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="user_name" label="名字"></el-table-column>
            <el-table-column prop="like_number" label="点赞数"></el-table-column>
            <el-table-column prop="content" label="内容" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="doctor" label="归属" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="created_at" label="发表时间"></el-table-column>
            <!--<el-table-column label="状态">-->
                <!--<template slot-scope="scope">-->
                    <!--{{scope.row.status | formatByOptions(options.status, 'value', 'text')}}-->
                <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column align="center" label="操作" width="250">
                <template slot-scope="scope">
                    <el-button size="small" type="info" >详情</el-button>
                    <el-button size="small" type="warning" >编辑</el-button>
                    <!--<router-link :to="{ path: '/article/update/' + scope.row.id }">-->
                    <!--<el-button size="small" type="warning">编辑</el-button>-->
                    <!--</router-link>-->
                    <el-button size="small" type="danger" @click="trashed(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination-component ref="pagination" v-on:getList="getList"></pagination-component>

        <!--添加弹框-->
        <el-dialog :title="formTitle" :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form class="small-space" :model="form" :rules="rules" ref="form" label-position="left" label-width="100px">
                <input type="hidden" v-model="form.id">
                <el-form-item label="头像/" prop="user_image">
                    <el-upload
                            class="avatar-uploader"
                            :action="importFileUrl"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :on-error="handleError"
                            :before-upload="beforeAvatarUpload"
                            :data="upLoadData"
                            :on-change="handleChange">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="用户昵称" prop="combos_name">
                    <el-input v-model="form.username" placeholder="请填写用户昵称"></el-input>
                </el-form-item>
                <el-form-item label="评论内容" prop="combos_name">
                    <el-input v-model="form.content" placeholder="请填写评论内容"></el-input>
                </el-form-item>
                <el-form-item label="点赞数" prop="combos_name">
                    <el-input v-model="form.likenumber" placeholder="请填写点赞数"></el-input>
                </el-form-item>


                <el-form-item label="状态" prop="status">
                <el-select v-model="form.model_id" placeholder="请选择哪位医生">
                <el-option label="全部" value="0"></el-option>
                <el-option v-for="item in doctorlist" :key="item.id" :label="item.doctor_name" :value="item.id"></el-option>
                </el-select>
                </el-form-item>
            </el-form>
            <dialog-footer-component v-on:submit="submit(formName = 'form')" v-on:close="close"></dialog-footer-component>
        </el-dialog>

    </div>
</template>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
<script>
    import PaginationComponent from '../../common/pagination-component.vue';
    import TableHeaderComponent from '../../common/table-header-component.vue';
    import DialogFooterComponent from '../../common/dialog-footer-component.vue';

    export default {
        components: {
            'pagination-component': PaginationComponent,
            'table-header-component': TableHeaderComponent,
            'dialog-footer-component': DialogFooterComponent,

        },
        data() {
            return {
                formTitle: '',
                importFileUrl: 'http://upload-z2.qiniup.com',
                imageUrl:'',
                doctorlist:'',
                upLoadData: {
                    token: '',
                },
                formVisible:false,
                tableData: [],
                searchForm: {
                    user_name: '',
                },
                form: {
                    id: '',
                    userimage: '',
                    username: '',
                    content: '',
                    likenumber: '',
                    model_id: '',
                },
                options: {
                    status: [],
                    categories: []
                }
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
            getList() {
                let _this = this;
                let search = {
                    'user_name': _this.searchForm.user_name,
                };
                let paramsData = { 'data': { 'search': search } };
                axios.get('/backend/comment?page=' + _this.$refs.pagination.pageData.current_page, { params: paramsData }).then(response => {
                    let { status, data, message } = response.data;
                    _this.tableData = data.lists.data;
                    _this.$refs.pagination.pageData.per_page = parseInt(data.lists.per_page);
                    _this.$refs.pagination.pageData.current_page = parseInt(data.lists.current_page);
                    _this.$refs.pagination.pageData.total = parseInt(data.lists.total);
                })
            },
            getUptoken() {
                let _this = this;
                axios.get('api/qiniu/token').then(response => {
                    let { status, data, message } = response.data;
                    _this.upLoadData.token= data.uptoken;
                });
            },
            getDoctor() {
                let _this = this;
                axios.get('api/doctor/list').then(response => {
                    let  data  = response.data;
                    _this.doctorlist= data;
                });
            },
            submit(formName) {
                let _this = this;
                _this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/backend/comment/create', { 'data': _this.form }).then(response => {
                            let {status, data, message} = response.data;
                            if (!status) {
                                _this.$message.error(message);
                                return false;
                            }
                            _this.$message.success(message);
                            //_this.$router.push({ path: '/brand/comment' });
                            _this.formVisible = false;
                            this.form.userimage='';
                            this.getList();
                        });
                    }
                });
            },
            handleAvatarSuccess(res, file) {
                //上传成功后在图片框显示图片
                this.imageUrl ='http://orrs4ojbz.bkt.clouddn.com/'+ res.key;
                this.form.userimage=res.key;
            },
            create() {
                let _this = this;
                _this.getDoctor();
                _this.formTitle = '查看详情';
                Vue.resetForm(_this.form);
                _this.formVisible = true;
                _this.getUptoken();

            },
            details(){

            },
            close(){
                let _this = this;
                _this.formVisible = false;
                this.form.userimage='';
            },
            trashed(id){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post('/backend/comment/destroy', { 'data': id }).then(response => {
                        let {status, data, message} = response.data;
                        if (!status) {
                            _this.$message.error(message);
                            return false;
                        }
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },


        }
    }
</script>